<template>
  <!-- 公共搜索 -->
  <div>
    <TopFilter ref="commonTopFilter" :list="topFilterInit" :cols="3" labelWidth="90" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!-- 经销商弹窗 -->
    <BaseDialog title="经销商查询" :visible.sync="visibleDealer" width="1000px" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <dealer-madal ref="addDialogRef" selectType="multiple" v-model="initDealerData" @close="closeDealer" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { region } from '@/api/cms-common/region'; //行政区域

import { getRegion } from '@/api/cms-common/dealerdata/index'; //事业部，办事处

import dealerMadal from '@/pages/cms-common/dealerdata'; //经销商选择框

export default {
  mixins: [dictionary, region],
  components: {
    dealerMadal
  },
  props: {
    defaultProjectStatus: {
      type: [Object, String],
      required: false,
      default: () => {
        return '';
      }
    },
    type: {
      type: String,
      default: () => {
        return 'none';
      }
    }
  },
  data() {
    return {
      visibleDealer: false,
      initDealerData: {}, //经销商弹窗参数
      topFilterInit: this.topFilterInitFun(),
      dealerCodes: []
    };
  },
  created() {
    if (this.type === 'inquire') {
      //报备查询页
      this.topFilterInit.push(
        // { type: 'SELECT', label: '事业部', fieldName: 'deptId', placeholder: '请选择', initialValue: '', itemList:[] }
        { type: 'SELECT', label: '事业部', fieldName: 'majorManagerId', placeholder: '请选择', initialValue: '', itemList: this.createDictList('2566') }
      );
    } else if (this.type === 'projectReportApply') {
      //报备申请页
      this.topFilterInit.splice(this.topFilterInit.length - 1, 1);
    } else if (this.type === 'selectList') {
      //如果是弹出专用页
      let projectNo = { type: 'INPUT', label: '项目编号', fieldName: 'projectCode', placeholder: '请输入', initialValue: '' };
      this.topFilterInit.splice(1, 5);
      this.topFilterInit.splice(this.topFilterInit.length - 1, 1);
      this.topFilterInit.splice(1, 0, projectNo);
    }
  },
  mounted() {
    //初始化事业部
    if (this.type === 'inquire') {
      //报备查询页
      // this.getDeptData();
    }
    //初始化省份
    this.initProvince();
  },
  methods: {
    //字符转数字，为空，或者NaN则返回null，
    transformProjectStatus(val) {
      let status = parseInt(val);
      if (isNaN(status)) {
        return null;
      } else {
        return status;
      }
    },
    //获取事业部下拉列表数据
    async getDeptData() {
      const res = await getRegion();
      if (res.resultCode === 200) {
        this.topFilterInit.find(item => item.fieldName === 'deptId').itemList = res.data.map(x => ({ value: parseInt(x.id), text: x.orgName }));
      }
    },
    //初始化所在省份
    async initProvince() {
      let pl = await this.createRegionList();
      this.topFilterInit.find(item => item.fieldName === 'customerProvince').itemList = pl;
    },

    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: '客户公司名称', fieldName: 'customerName', placeholder: '请输入', initialValue: '' },
        { type: 'SELECT', label: '客户类型', fieldName: 'customerType', placeholder: '请选择', initialValue: '', itemList: this.createDictList('2506') },
        { type: 'SELECT', label: '客户性质', fieldName: 'customerProperty', placeholder: '请选择', initialValue: '', itemList: this.createDictList('2534') },
        { type: 'SELECT', label: '省份', fieldName: 'customerProvince', placeholder: '请选择', initialValue: '', itemList: [] },
        { type: 'SELECT', label: '客户状态', fieldName: 'customerStatus', placeholder: '请选择', initialValue: '', itemList: this.createDictList('2507') },
        { type: 'SELECT', label: '客户来源', fieldName: 'customerSource', placeholder: '请选择', initialValue: '', itemList: this.createDictList('2508') },
        { type: 'INPUT', label: '项目名称', fieldName: 'projectName', placeholder: '请输入', initialValue: '' },
        {
          type: 'SELECT',
          label: '项目状态',
          fieldName: 'projectStatus',
          placeholder: '请选择',
          disabled: this.type === 'review',
          initialValue: this.transformProjectStatus(this.defaultProjectStatus),
          itemList: this.createDictList('2516')
        },
        {
          type: 'INPUT',
          label: '经销商',
          fieldName: 'dealerName',
          placeholder: '请选择',
          initialValue: '',
          readonly: true,
          unitRender: () => {
            // <el-divider direction="vertical" />
            return (
              <div>
                <el-button slot="append" icon="el-icon-search" onClick={() => this.dealerHandle()}></el-button>
                <el-button slot="append" icon="el-icon-delete" onClick={() => this.clearDealer()}></el-button>
              </div>
            );
          }
        }
        //项目生效日期和保护期到期日
        // { type: 'RANGE_DATE', label: '项目生效期', fieldName: 'projectDate', placeholder: '请选择', initialValue: '' },
      ];
    },
    // 经销商关闭窗口 -返回数据
    closeDealer(data) {
      let dealerCodes = [];
      let dealerNames = [];
      if (data && data.data && data.data.length > 0) {
        for (let i in data.data) {
          dealerCodes.push(data.data[i].dealerCode);
          dealerNames.push(data.data[i].dealerName);
        }
      }

      this.topFilterInit.find(item => item.fieldName === 'dealerName').initialValue = dealerNames.join(',');
      this.dealerCodes = dealerCodes;
      this.visibleDealer = false;
    },
    dealerHandle() {
      this.initDealerData = {
        excludeBigCustomer: false //是否查询大客户经销商，如果查询 true，不查询 false
      };
      this.visibleDealer = true;
    },
    clearDealer() {
      this.topFilterInit.find(item => item.fieldName === 'dealerName').initialValue = '';
      this.dealerCodes = [];
    },
    //搜索获取数据
    changeHandle(data) {
      //如果被清空了，则关联删除IDS和Names
      //防止后面更改，复制一个对象，后面的会覆盖前面的值
      let searchData = Object.assign({}, data);
      if (searchData.dealerName) {
        searchData.dealerCodes = this.dealerCodes.join(',');
      } else {
        searchData.dealerCodes = '';
      }
      if (searchData.projectDate) {
        searchData.searchProjectStartDate = searchData.projectDate[0];
        searchData.searchProjectEndDate = searchData.projectDate[1];
      } else {
        searchData.projectStartDate = null;
        searchData.projectEndDate = null;
      }
      this.$emit('filterChange', searchData);
    },
    //展开收起
    collapseHandle() {
      this.$emit('onCollapse');
    }
  }
};
</script>

<style lang="less" scoped></style>
